package com.qrcode.controller;

import java.io.File;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.qrcode.config.LogoConfig;
import com.qrcode.service.CoderService;
import com.qrcode.service.ZxingDecoderService;


@RestController
public class ZxingEncoderAction {

    private LogoConfig logiconfig ;
    private ZxingDecoderService zxingDecodeService ;
    private CoderService coderService ;

    @RequestMapping(value="/zxingdecode", method=RequestMethod.GET)
    public ModelAndView zxingdecode(@RequestParam("realImgPath") String realImgPath, HttpSession session) {
        String uploadPath = "/images" ;
        String realUploadPath = session.getServletContext().getRealPath(uploadPath) ;
        String imgPath = realUploadPath+"/"+realImgPath ;
        String result = zxingDecodeService.zxingdecode(imgPath) ;

        ModelAndView ret = new ModelAndView() ;
        ret.addObject("result", result) ;
        ret.setViewName("zxingdecode");

        return ret ;
    }


    @RequestMapping(value="/zxingcoder", method=RequestMethod.GET)
    public ModelAndView watermark(HttpSession session) throws Exception {
        String uploadPath = "/images" ;
        String realUploadPath = session.getServletContext().getRealPath(uploadPath) ;
        String imageName = "12345"+".png" ;
        // 模拟订单详情
        String contents = "订单编号：20160512082345"+"\n"+"订单金额：￥ 2050.00"+"\n"+"支付方式：预存款"+"\n"+"配送方式：京东快递" ;  

        int width = 300;  
        int height = 300; 
        String zxingImage = coderService.encode(contents, width, height, uploadPath, realUploadPath, imageName);  

        File image = new File(realUploadPath+"/"+imageName); 
        String logoImageUrl = logiconfig.LogoMatrix(image, uploadPath, realUploadPath, imageName) ;

        ModelAndView ret = new ModelAndView() ;
        ret.addObject("imageUrl", zxingImage) ;
        ret.addObject("imageName", imageName) ;
        ret.addObject("logoImageUrl", logoImageUrl) ;
        ret.setViewName("zxingcoder");

        return ret ;
    }
    
    
    @GetMapping(value="/erweima")
    public void erweima() throws Exception {
        String uploadPath = "E:/tmp" ;
        String realUploadPath = "E:/tmp";
        String imageName = "12345"+".png" ;
        // 模拟订单详情
        String contents = "订单编号：20160512082345"+"\n"+"订单金额：￥ 2050.00"+"\n"+"支付方式：预存款"+"\n"+"配送方式：京东快递" ;  

        int width = 300;  
        int height = 300; 
        String zxingImage = coderService.encode(contents, width, height, uploadPath, realUploadPath, imageName);  
        System.out.println("生成二维码的路径为:" + zxingImage);
        File image = new File(realUploadPath+"/"+imageName); 
        String logoImageUrl = logiconfig.LogoMatrix(image, uploadPath, realUploadPath, imageName) ;
        System.out.println("生成二维码加上logo的路径为:" + logoImageUrl);
    }
    
    

    @Autowired
    public void setLogiconfig(LogoConfig logiconfig) {
        this.logiconfig = logiconfig;
    }

    @Autowired
    public void setZxingDecodeService(ZxingDecoderService zxingDecodeService) {
        this.zxingDecodeService = zxingDecodeService;
    }

    @Autowired
    public void setCoderService(CoderService coderService) {
        this.coderService = coderService;
    }  

}
